package com.zeng.jiudian.servlet.news;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zeng.jiudian.bean.NewModel;
import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.service.NewService;
import com.zeng.jiudian.service.impl.NewServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/27 - 16:09
 */
@WebServlet("/back/new/findAll")
public class NewListServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        String title = req.getParameter("title");
        String from = req.getParameter("from");
        String author = req.getParameter("author");
        String type = req.getParameter("type");
        String content = req.getParameter("content");
        String introduction = req.getParameter("introduction");
        String startBirthday = req.getParameter("startBirthday");
        String endBirthday = req.getParameter("endBirthday");
        String order = req.getParameter("order");
        String lift = req.getParameter("lift");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        String nowNum = req.getParameter("nowNum");
        String isShow = req.getParameter("isShow");
        String recommend = req.getParameter("recommend");
        String newType = req.getParameter("newType");

        NewModel newModel = new NewModel();
        newModel.setTitle(title);
        newModel.setIsFrom(from);
        newModel.setAuthor(author);
        newModel.setType(type);
        newModel.setContent(content);
        newModel.setStartCreateTime(startBirthday);
        newModel.setEndCreateTime(endBirthday);
        newModel.setOrder(order);
        newModel.setLift(lift);
        newModel.setIntroduction(introduction);
        newModel.setIsShow(isShow == null ? null : Integer.parseInt(isShow));
        newModel.setRecommend(recommend == null ? null : Integer.parseInt(recommend));
        newModel.setNewTypeId(newType == null ? 0 : Integer.parseInt(newType));

        PageModel pageModel = new PageModel();
        pageModel.setPageNum((pageNum == null || "".equals(pageNum) || "0".equals(pageNum)) ? 1 : Integer.parseInt(pageNum));
        pageModel.setPageSize((pageSize == null || "".equals(pageSize) || "0".equals(pageSize)) ? 10 : Integer.parseInt(pageSize));
        pageModel.setNowNum((nowNum == null || "".equals(nowNum) || "0".equals(nowNum)) ? 1 : Integer.parseInt(nowNum));
        newModel.setPageModel(pageModel);

        NewService newService = new NewServiceImpl();
        List<NewModel> list = newService.findAll(newModel);
        int count = newService.getCount(newModel);

        String listString = JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect);
        JSONArray listArray = JSONArray.parseArray(listString);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", listArray);
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
